;Ferruh Mavituna 

;--------------------------------
;Include Modern UI
!include "MUI.nsh"
 
!include "WordFunc.nsh"
;!insertmacro WordReplace
;!insertmacro WordFind

;!define MUI_HEADERIMAGE_BITMAP_NOSTRETCH
;!define MUI_WELCOMEFINISHPAGE_BITMAP "Resources\Branding.bmp"
;!define MUI_HEADERIMAGE
;!define MUI_HEADERIMAGE_BITMAP "Resources\Branding.bmp"

;!define MUI_ICON "..\Resources\picon.ico"
;!define MUI_UNICON "Resources\Installer-Icon-simple.ico"




;--------------------------------
;General	
;SetCompress "Off"
SetCompressor /SOLID lzma

  ;Name and file
  Name "Psycho Folder"
  OutFile "PsychoFolderSetup.exe"
  BrandingText "Psycho Folder Installer"
  
  ;Default installation folder
  InstallDir "$PROGRAMFILES\Ferruh Mavituna\Psycho Folder"
  
  ;Get installation folder from registry if available
  InstallDirRegKey HKCU "Software\PsychoFolder" ""

  ;Vista redirects $SMPROGRAMS to all users without this
  RequestExecutionLevel admin
  
  
!insertmacro MUI_PAGE_COMPONENTS
	
	
	;AddBrandingImage bottom 35
	
  
	Section
		Call IsDotNETInstalled
		Pop $R3
		
		StrCmp $R3 0 +3
		Goto +3
		; else
			MessageBox MB_OK ".NET Framework 2.0 is not installed. Installer will continue but you may need to download .NET Framework 2.0 before run Psycho Folder"
			ExecShell "open" "http://www.microsoft.com/downloads/details.aspx?familyid=0856eacb-4362-4b0d-8edd-aab15c5e04f5&displaylang=en"
	SectionEnd
	
	

;--------------------------------
;Variables

  Var MUI_TEMP
  Var STARTMENU_FOLDER

;--------------------------------
;Interface Settings

  !define MUI_ABORTWARNING

;--------------------------------
;Pages

  !insertmacro MUI_PAGE_LICENSE "Licence.txt"
  ;!insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_DIRECTORY
  
  ;Start Menu Folder Page Configuration
  !define MUI_STARTMENUPAGE_REGISTRY_ROOT "HKCU" 
  !define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\PsychoFolder" 
  !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Start Menu Folder"
  
  !insertmacro MUI_PAGE_STARTMENU Application $STARTMENU_FOLDER
  
  !insertmacro MUI_PAGE_INSTFILES
  
  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES

;--------------------------------
;Languages
 
  !insertmacro MUI_LANGUAGE "English"

;--------------------------------
;Installer Sections

Section "Psycho Folder" Section1

  SectionIn RO
  SetOutPath "$INSTDIR"
  SetOverwrite On
  
  ;Core Files
  File "..\bin\Release\PsychoFolder.exe"
  File "..\bin\Release\Rules.xml"
    
     
  ;Store installation folder
  WriteRegStr HKCU "Software\PsychoFolder" "" $INSTDIR
  
  ;Create uninstaller
  WriteUninstaller "$INSTDIR\Uninstall.exe"
  
  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
    
    ;Create shortcuts
    CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER"        
    CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
		
	!insertmacro MUI_STARTMENU_WRITE_END
	
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\PsychoFolder" \
                 "DisplayName" "Psycho Folder"	
	
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\PsychoFolder" \
                 "UninstallString" "$\"$INSTDIR\uninstall.exe$\""
	
	;Run the application
	Exec '"$INSTDIR\PsychoFolder.exe"'

SectionEnd

Section "Add to Windows Startup" Section2
    CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Psycho Folder.lnk" "$INSTDIR\PsychoFolder.exe"
SectionEnd

LangString DESC_Section1 ${LANG_ENGLISH} "Psycho Folder Required Files"
LangString DESC_Section2 ${LANG_ENGLISH} "Add Psycho Folder to Windows Startup"

!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
  !insertmacro MUI_DESCRIPTION_TEXT ${Section1} $(DESC_Section1)
  !insertmacro MUI_DESCRIPTION_TEXT ${Section2} $(DESC_Section2)
!insertmacro MUI_FUNCTION_DESCRIPTION_END


 
;--------------------------------
;Uninstaller Section

Section "Uninstall"

  RMDir /r "$INSTDIR"
  
  !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP
    
  Delete "$SMPROGRAMS\$MUI_TEMP\*.*"
  Delete "$SMPROGRAMS\$STARTMENU_FOLDER\PsychoFolder.lnk"
    
  ;Delete empty start menu parent diretories
  StrCpy $MUI_TEMP "$SMPROGRAMS\$MUI_TEMP"
 
  startMenuDeleteLoop:
	ClearErrors
    RMDir $MUI_TEMP
    GetFullPathName $MUI_TEMP "$MUI_TEMP\.."
    
    IfErrors startMenuDeleteLoopDone
  
    StrCmp $MUI_TEMP $SMPROGRAMS startMenuDeleteLoopDone startMenuDeleteLoop
  startMenuDeleteLoopDone:

  DeleteRegKey /ifempty HKCU "Software\PsychoFolder"
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\PsychoFolder"


SectionEnd


Function IsDotNETInstalled
  Push $0
  Push $1
  Push $2
  Push $3
  Push $4
 
  ReadRegStr $4 HKEY_LOCAL_MACHINE \
    "Software\Microsoft\.NETFramework" "InstallRoot"
  # remove trailing back slash
  Push $4
  Exch $EXEDIR
  Exch $EXEDIR
  Pop $4
  # if the root directory doesn't exist .NET is not installed
  IfFileExists $4 0 noDotNET
 
  StrCpy $0 0
 
  EnumStart:
 
    EnumRegKey $2 HKEY_LOCAL_MACHINE \
      "Software\Microsoft\.NETFramework\Policy"  $0
    IntOp $0 $0 + 1
    StrCmp $2 "" noDotNET
 
    StrCpy $1 0
 
    EnumPolicy:
 
      EnumRegValue $3 HKEY_LOCAL_MACHINE \
        "Software\Microsoft\.NETFramework\Policy\$2" $1
      IntOp $1 $1 + 1
       StrCmp $3 "" EnumStart
        IfFileExists "$4\$2.$3" foundDotNET EnumPolicy
 
  noDotNET:
    StrCpy $0 0
    Goto done
 
  foundDotNET:
    StrCpy $0 1
 
  done:
    Pop $4
    Pop $3
    Pop $2
    Pop $1
    Exch $0
FunctionEnd